#{extends '/main.html' /}
<div>
	<script type="text/javascript" src="@{'/public/js/jquery.form.js'}"></script>
	<form action="@{manager.ShopItemController.add}" method="post"
		enctype="multipart/form-data" id="addItemForm">
		<table>
			<tr>
				<td align="center" colspan="2" style="color: red;" id="err">${error_msg}</td>
			</tr>
			<tr>
				<td>名称</td>
				<td><input type="text" name="itemName" size="50" /><font
					color="red">*</font></td>
			</tr>
			<tr>
				<td>商品图片</td>
				<td valign="bottom">
					<div style="display: inline; float: left">
						<span id="fileContainer" style="display: inline; float: left"><input
							type="radio" name="imageType" value="1" checked="checked" />网络图片<input
							type="radio" name="imageType" value="2" />本地图片<br /> <input
							type="text" name="netImage" id="fileInput" size="100" /> </span><span
							style="display: inline; float: left; color: red;"><br />
						<br />*</span>
					</div>
				</td>
			</tr>
			<tr>
				<td>描述</td>
				<td><input type="text" name="description" /></td>
			</tr>
			<tr>
				<td>市场价格</td>
				<td><input type="text" name="marketPrice" /><font color="red">*</font></td>
			</tr>
			<tr>
				<td>销售价格</td>
				<td><input type="text" name="price" /><font color="red">*</font></td>
			</tr>
			<tr>
				<td>类型</td>
				<td><select name="type"><option value="1">男童装</option>
						<option value="2">女童装</option></select><font color="red">*</font></td>
			</tr>
			<tr>
				<td>详情地址</td>
				<td><input type="text" size="50" name="detailUrl"/><font color="red">*</font></td>
			</tr>
			<tr>
				<td></td>
				<td align="center"><input type="submit" value="添加" id="addBtn" /></td>
			</tr>
		</table>
	</form>
	<script type="text/javascript">
		var addTip = '${tip}';
		if (addTip.length > 0) {
			if (addTip == "success") {
				alert('添加成功');
			} else {
				alert(addTip);
			}
		}
		$("#fileContainer :radio").click(function() {
			var typeRadio = $("#fileContainer :radio");
			$("#fileContainer")[0].removeChild($("#fileInput")[0]);
			var input = $(document.createElement("input"));
			input.attr("id", "fileInput");
			if (typeRadio[0].checked) {
				input.attr("type", "text");
				input.attr("name", "netImage");
				input.attr("size", "100");
			} else {
				input.attr("type", "file");
				input.attr("name", "imagePath");
			}
			$("#fileContainer").append(input);
		});

		$("#addItemForm tr td:first-child").css("textAlign", "right");
		$("#addItemForm tr td:last-child").css("textAlign", "left");
		$("#addItemForm tr td:last-child:first-child").css("marginRight", "5");
		$("#addItemForm").submit(
				function(e) {
					if ($("input[name='itemName']").val().length <= 0) {
						$("#err").text("请输入商品名称");
						return false;
					}
					var typeRadio = $("#fileContainer :radio");
					if (typeRadio[1].checked) {
						var file = $("input[name='imagePath']").val();
						if (file.length <= 0) {
							$("#err").text("请选择一个商品图片");
							return false;
						}
						var ext = file.substring(file.lastIndexOf('.') + 1)
								.toLowerCase();
						if (ext != 'jpg' && ext != 'jpeg' && ext != 'png'
								&& ext != 'bmp' && ext != 'gif') {
							$("#err").text("图片格式不正确，请选择一个有效的图片");
							return false;
						}
					} else {
						var file = $("input[name='netImage']").val();
						if (file.lenth == 0) {
							$("#err").text("请输入一个网络图片的地址");
							return false;
						}
					}
					var marketPrice = parseFloat($("input[name='marketPrice']")
							.val());
					if (isNaN(marketPrice)) {
						$("#err").text("市场价格不是一个有效的数字");
						return false;
					}
					var price = parseFloat($("input[name='price']").val());
					if (isNaN(price)) {
						$("#err").text("销售价格不是一个有效的数字");
						return false;
					}
					if (price > marketPrice) {
						$("#err").text("销售价格不能高于市场价格");
						return false;
					}
					if ( $("input[name='detailUrl']").val().lenth <= 0) {
						$("#err").text("请输入商品查看详情网址");
						return false;
					}

					$("#err").text("");
					return true;
				});
	</script>
</div>